<?php

require_once "config.php";

$params = new UrlParams();
if(!isset($_POST['query'])) $_POST['query'] = null;

Template::$currentServer = $config->servers[(int)$params->currentServer];

/** @var MysqlDatabase */
$DB = ADatabase::create(Template::$currentServer);

$DB->getDatabases();

Template::header();

echo '<nav id="navpanel">';
	$databases = $DB->getDatabases();
	echo '<ul class="servers">';
		$serversCounter = 0;
		foreach($config->servers as $server)
		{
			echo '<li class="';
			if($serversCounter == $params->currentServer)
			{
				echo 'currentServer';
			}
			echo ' server">';
				echo '<a href="?'.$params->link(array("currentServer"=>$serversCounter,"currentDatabase"=>null,"mainPanel"=>"showServer")).'">'.H($server->hostname).'</a>';
				if($serversCounter == $params->currentServer)
				{
					echo '<ul class="databases">';
						foreach($databases as $database)
						{
							echo '<li class="';
							if($database == $params->currentDatabase)
							{
								echo 'currentDatabase';
							}
							echo '"><a href="?'.$params->link(array("currentDatabase"=>$database,"currentTable"=>null,"mainPanel"=>"showDatabase")).'">'.H($database).'</a>';
							if($database == $params->currentDatabase)
							{
								$tables = $DB->getTables($database);
								echo '<ul class="tables">';
								foreach($tables as $table)
								{
									echo '<li class="';
									if($database == $params->currentDatabase && $table == $params->currentTable)
									{
										echo 'currentDatabase';
									}
									echo '"><a href="?'.$params->link(array("currentDatabase"=>$database,"currentTable"=>$table,"mainPanel"=>"showTable")).'">'.H($table).'</a></li>';
								}
								echo '</ul>';
							}
							echo '</li>';
						}
					echo '</ul>';
				}
			echo '</li>';
			$serversCounter++;
		}
	echo '</ul>';
echo '</nav>';

if($params->currentDatabase) $DB->useDatabase($params->currentDatabase);

echo '<section id="main">';
	echo '<section id="mainpanel">';
		echo '<nav id="breadcrumb">';
			echo '<ul>';
				$p = new UrlParams(false);
				if(Template::$currentServer)
				{
					$p->currentServer = $params->currentServer;
					$p->mainPanel = "showServer";
					echo '<li><a href="?'.$p->link().'">'.H(Template::$currentServer->hostname).'</a></li>';
				}
				if($params->currentDatabase)
				{
					$p->currentDatabase = $params->currentDatabase;
					$p->mainPanel = "showDatabase";
					echo '<li><a href="?'.$p->link().'">'.H($params->currentDatabase).'</a></li>';
				}
				
				if($params->currentTable)
				{
					$p->currentTable = $params->currentTable;
					$p->mainPanel = "showTable";
					echo '<li><a href="?'.$p->link().'">'.H($params->currentTable).'</a></li>';
				}
			echo '</ul>';
		echo '</nav>';
		$tabs = array();
		echo '<div id="paneltabs">';
			echo '<ul>';
				if($params->mainPanel == "query")
				{
					echo '<li><a ';
						echo 'href="#tab-result" ';
						echo 'data-for="#tab-result" ';
					echo '>Result</a></li>';
					$tabs[] = "result";
				}
				if($params->currentTable)
				{
					echo '<li><a ';
						echo 'href="?'.$params->link(array("mainPanel"=>"browse")).'" ';
						echo 'data-link="?'.$params->link(array("mainPanel"=>"browse")).'" ';
						echo 'data-for="#tab-browse" ';
					echo '>Browse</a></li>';
					$tabs[] = "browse";
					echo '<li><a ';
						echo 'href="?'.$params->link(array("mainPanel"=>"showTable")).'" ';
						echo 'data-link="?'.$params->link(array("mainPanel"=>"showTable")).'" ';
						echo 'data-for="#tab-structure" ';
					echo '>Structure</a></li>';
					$tabs[] = "structure";
				}
				elseif($params->currentDatabase)
				{
					echo '<li><a ';
						echo 'href="?'.$params->link(array("mainPanel"=>"showDatabase")).'" ';
						echo 'data-link="?'.$params->link(array("mainPanel"=>"showDatabase")).'" ';
						echo 'data-for="#tab-structure" ';
					echo '>Structure</a></li>';
					$tabs[] = "structure";
				}
				elseif(Template::$currentServer)
				{
					echo '<li><a ';
						echo 'href="?'.$params->link(array("mainPanel"=>"showServer")).'" ';
						echo 'data-link="?'.$params->link(array("mainPanel"=>"showServer")).'" ';
						echo 'data-for="#tab-databases" ';
					echo '>Databases</a></li>';
					$tabs[] = "databases";
				}
			echo '</ul>';
			if($params->mainPanel == "query")
			{
				$currentTab = "result";
				echo '<div id="tab-result">';
					$DB->getQueryResult($_POST['query'])->displayResult();
				echo '</div>';
			}
			if($params->mainPanel == "browse")
			{
				$currentTab = "browse";
				echo '<div id="tab-browse">';
					$DB->browseResult($params->currentDatabase, $params->currentTable)->displayResult();
				echo '</div>';
			}
			if($params->mainPanel == "showTable")
			{
				$currentTab = "structure";
				echo '<div id="tab-structure">';
					$result = $DB->tableColumns($params->currentDatabase, $params->currentTable);
					$result->displayQueryDetails();
					echo '<h1>Table Structure</h1>';
					$result->displayQueryResults();
					echo '<h1>Indexes</h1>';
					$DB->tableIndexes($params->currentDatabase, $params->currentTable)->displaySimpleTable();
				echo '</div>';
			}
			if($params->mainPanel == "showDatabase")
			{
				$currentTab = "structure";
				echo '<div id="tab-structure">';
					$DB->mainPanelDatabase($params->currentDatabase);
				echo '</div>';
			}
			if($params->mainPanel == "showServer")
			{
				$currentTab = "databases";
				echo '<div id="tab-databases">';
					$DB->mainPanelServer(Template::$currentServer);
				echo '</div>';
			}
		echo '</div>';
		$currentTabIndex = 0;
		if($currentTab) $currentTabIndex = array_search($currentTab, $tabs);
		?><script>
		$(function() {
			$('#paneltabs ul li a[data-for]').attr("href", function() { return $(this).attr("data-for"); });
			$('#paneltabs').tabs({
				'selected': <?=$currentTabIndex?>
			});
			$('#paneltabs ul li a[data-link]').attr("href", function() { return $(this).attr("data-link"); });
		});
		</script><?
	echo '</section>';
	echo '<section id="querypanel">';
		echo '<form action="?'.$params->link(array("mainPanel"=>"query")).'" method="POST">';
			echo '<table><tr><td>';
				echo '<textarea name="query" id="queryText">';
					if(DatabaseResult::$currentQuery)
					{
						echo H(DatabaseResult::$currentQuery);
					}
					elseif($_POST['query'])
					{
						H($_POST['query']);
					}
				echo '</textarea>';
			echo '</td><td id="queryButton">';
				echo '<input type="submit" value="Run">';
			echo '</td></tr></table>';
		echo '</form>';
	echo '</section>';
echo '</section>';

Template::footer();